package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.entity.Store;
import com.gudaomai.wuye.entity.StoreInput;
import com.gudaomai.wuye.service.IStoreServ;
import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/store")
public class StoreCtrl extends BaseController {
	
	@Autowired
	IStoreServ storeService;
	
	/** 
	 * add a new Store
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if the store with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The store settings page, click the add button, add store")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addStore(@RequestBody StoreInput storeInput) throws Exception {

        RetEntity entity = new RetEntity();

		if(storeService.isExist(storeInput.getCode())){
			// The store already exists in the database
			
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
					getMessage(MsgConstant.RET_STORE_ADD_FAILURE));
		} 
		else{
			// Create new store entity
			Store storeEntity = new Store();
			// copy all user inputs to the new store entity
			BeanUtils.copyProperties(storeInput, storeEntity);
			
			// Insert the store entity into the database
			storeService.insert(storeEntity);
			
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_STORE_ADD_SUCCESS));
		}	
		
		return entity;
		
		
	}
	
	/** 
	 * query Store information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The store settings page, click the query button, query store using store's id")	
	@GetMapping("/id/{id}")
	public Store getStorebyId(@PathVariable("id") String id) throws Exception{
		
		Store store = storeService.selectById(id);
		
		store.setRetCode(CommonConstant.RESULT_CODE_OK);
		store.setRetMessage(getMessage(MsgConstant.RET_STORE_SELECT_FAILURE));
		return store;
	}
	
	/** 
	 * update the existing Store information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update the Store information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The store settings page, click the modify button, update store")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RetEntity updateStore(@RequestBody StoreInput storeInput) throws Exception{
				
        RetEntity entity = new RetEntity();
		
		// update the store entity in the database
		// Create new store entity
		Store storeEntity = new Store();
		// copy all user inputs to the new commRegion entity
		BeanUtils.copyProperties(storeInput, storeEntity);
		
		storeService.updateById(storeEntity);
		
		//Return message and HTTP result code
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_STORE_UPDATE_SUCCESS));
		
		return entity;
	}
	
	
	/** 
	 * delete the existing Store
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete the Store information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The store  settings page, click the delete button, delete House")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delStorebyId(@PathVariable("id") String id) throws Exception{
		
        RetEntity entity = new RetEntity();
		
		storeService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_STORE_DELETE_SUCCESS));
		return entity;
	}
}
